package com.rbac.web.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rbac.web.db.RolePermissionDAO;
import com.rbac.web.db.RolePermissionDO;
import com.rbac.web.model.RolePermission;
import com.rbac.web.service.IRolePermissionService;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author gengaoliu
 */
@Service
@RequiredArgsConstructor
public class RolePermissionServiceImpl implements IRolePermissionService {
  final RolePermissionDAO dao;

  @Override
  public void insert(RolePermission rolePermission) {
    dao.insert(RolePermissionDO.from(rolePermission));
  }

  @Override
  public void deleteAllByRoleId(Integer roleId) {
    dao.deleteByRoleId(roleId);
  }

  @Override
  public List<RolePermission> findByRoleId(Integer roleId) {
    LambdaQueryWrapper<RolePermissionDO> wrapper =
        Wrappers.<RolePermissionDO>lambdaQuery().eq(RolePermissionDO::getRoleId, roleId);
    return dao.selectList(wrapper).stream().map(RolePermissionDO::to).toList();
  }
}
